package com.huayi.website.controller.manage;

import com.huayi.website.entity.BannerEntity;
import com.huayi.website.entity.NewsCategoryEntity;
import com.huayi.website.request.GoodsDeleteReq;
import com.huayi.website.service.BannerService;
import com.huayi.website.utils.EnumResultVo;
import com.huayi.website.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/manage")
@Controller
public class MBannerController {
    @Autowired
    private BannerService bannerService;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "/banner_list", method = RequestMethod.GET)
    public String getList(HttpServletRequest request, Model model) {

        var result = bannerService.getAllList(null);
        model.addAttribute("list", result);
        model.addAttribute("totalSize", result.size());

        model.addAttribute("menu","banner_list");
        return "manage/banner_list";
    }



    /**
     * 添加分类
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/banner_add", method = RequestMethod.GET)
    public String add(HttpServletRequest request, Model model) {

        model.addAttribute("banner",new BannerEntity());
        model.addAttribute("menu","banner_list");

        return "manage/banner_add";
    }

    @RequestMapping(value = "/banner_edit", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, @RequestParam Integer id, Model model) {
        var banner = bannerService.getById(id);
        model.addAttribute("banner",banner);

        model.addAttribute("menu","banner_list");
        return "manage/banner_add";
    }

    @ResponseBody
    @RequestMapping(value = "/banner_add_submit", method = RequestMethod.POST)
    public ResultVo<String> publishPost(Model model, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String userName = session.getAttribute("userName").toString();
        if (StringUtils.isEmpty(userName)) return new ResultVo<>(500, "请登录");


        String title = request.getParameter("title");
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)) id="0";
        String sort = request.getParameter("sort");
        String url = request.getParameter("url");
        Integer columnId = Integer.parseInt(request.getParameter("columnId"));

        //图片
        String images = request.getParameter("image");
        if (StringUtils.isEmpty(images)) {
            //商品主图不能为空
            return new ResultVo<>(200, "请上传图片");
        }


        Integer bannerId = bannerService.editBanner(Integer.parseInt(id),title,url,Integer.parseInt(sort),images,columnId);

        return new ResultVo<>(0, "SUCCESS");
    }

    /**
     * 删除
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/banner_delete", method = RequestMethod.POST)
    public ResultVo<Integer> delete(@RequestBody GoodsDeleteReq req) {
        if (req.getId() == null || req.getId() <= 0)
            return new ResultVo<>(EnumResultVo.ParamsError.getIndex(), "参数错误，缺少id");

        bannerService.deleteBanner(req.getId());

        return new ResultVo<>(EnumResultVo.SUCCESS);
    }
}
